package com.example.hehecat.yuejian1.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.hehecat.yuejian1.R;
import com.oragee.banners.BannerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class MainFragment extends Fragment {
    @BindView(R.id.tl_tab)
    TabLayout tlTab;
    Unbinder unbinder;
    @BindView(R.id.vp_content)
    ViewPager vpContent;
    @BindView(R.id.banner)
    BannerView banner;
    private int[] imgs = {R.drawable.lb1, R.drawable.lb2, R.drawable.lb3, R.drawable.lb4};
    private List<View> viewList;
    private List<String> tabIndicators;
    private List<Fragment> tabFragments;
    private ContentPagerAdapter contentAdapter;
    private int bannerId;

    @OnClick(R.id.banner)
    void toInfo() {
        ActivityInfoFragment activityInfoFragment = ActivityInfoFragment.newInstance(bannerId);
        getActivity().getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.id_content, activityInfoFragment, null)
                .commit();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main, container, false);
        unbinder = ButterKnife.bind(this, view);

        initContent();
        initTab();
        banner();
        return view;
    }

    private void banner() {
        viewList = new ArrayList<View>();
        for (int i = 0; i < imgs.length; i++) {
            ImageView image = new ImageView(getContext());
            image.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            //设置显示格式
            image.setScaleType(ImageView.ScaleType.CENTER_CROP);
            image.setImageResource(imgs[i]);
            int bannerID = i;
            image.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    ActivityInfoFragment activityInfoFragment = ActivityInfoFragment.newInstance(bannerID+1);
                    getActivity().getSupportFragmentManager()
                            .beginTransaction()
                            .replace(R.id.id_content, activityInfoFragment, null)
                            .commit();
                }
            });
            viewList.add(image);
        }


        banner.setViewList(viewList);
        banner.startLoop(true);
    }



    private void initContent() {
        tabIndicators = new ArrayList<>();
        tabIndicators.add("活动");
        tabIndicators.add("大师");
        tabIndicators.add("比赛");
        tabFragments = new ArrayList<>();
        tabFragments.add(ActivityListFragment.newInstance("活动"));
        tabFragments.add(MasterListFragment.newInstance("大师"));
        tabFragments.add(GameListFragment.newInstance("比赛"));

        contentAdapter = new ContentPagerAdapter(getFragmentManager());
        vpContent.setAdapter(contentAdapter);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    public static MainFragment newInstance(String name) {
        MainFragment fragment = new MainFragment();
        Bundle args = new Bundle();
        args.putString("name", name);
        fragment.setArguments(args);
        return fragment;
    }

    private void initTab() {
        ViewCompat.setElevation(tlTab, 10);
        tlTab.setupWithViewPager(vpContent);
    }


    class ContentPagerAdapter extends FragmentStatePagerAdapter {

        public ContentPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return tabFragments.get(position);
        }

        @Override
        public int getCount() {
            return tabIndicators.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return tabIndicators.get(position);
        }

    }

}


